package com.roc.web.action.settlemgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.FreezeBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblSettleItem;
import com.roc.service.StrongDeductionsService;
import com.roc.service.TmpStrongDeducService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
@Component
@Namespace("/tmpDeduc")
@Scope("prototype")
public class TemStrongDeductionAction extends PaginationBaseAction{
	
	private static Logger logger = LogManager.getLogger(TemStrongDeductionAction.class);
	
	@Autowired
	private IPageService pagenitionService;
	
	@Autowired
	private StrongDeductionsService strongDeductionsService;
	
	@Autowired
	private TmpStrongDeducService tmpStrongDeducService;
	
	private TemStrongDeductionSearchCondition searchCondition;
	
	private FreezeBean freezeBean;
	
	/**
	 * 初始化待出款页面
	 * @return
	 */
	@Action(value="querytmpDeducInit",results={@Result(name="success",location="/pages/settlement/tmpStrongDecductions.jsp"),
			@Result(name = "error", location = "/error.jsp")})
	public String queryMakeMoneyJsp(){
		try {
			Operators op = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
				|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
				|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
				searchCondition=new TemStrongDeductionSearchCondition();
				String today = Constance.currTime();
				searchCondition.setApplyDateMin(today);
				searchCondition.setApplyDateMax(today);
				
				return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("初始化临时强扣页面异常");
			return ERROR;
		}
		return ERROR;		
	}
	/**
	 * 待出款列表
	 * @return
	 */
	@Action(value="queryTmpDeducList",results={@Result(name="success",location="/pages/settlement/tmpStrongDecductions.jsp")})
	public String querySettlementMsgList(){
		try {
			Operators op = (Operators) getSession().getAttribute(
					Constant.SESSION_LOGIN_MERCHANT);
			if (op.getRoleType().equals(Role.CLEAR_OPERATORS.getCode())
					|| op.getRoleType().equals(Role.CLEAR_LEAD.getCode())
					|| op.getRoleType().equals(Role.SYSTEM.getCode())) {
				if (searchCondition == null) {
					searchCondition = (TemStrongDeductionSearchCondition) getSearchConditionFromSession();
					if (searchCondition == null) {
						searchCondition = new TemStrongDeductionSearchCondition();
					}
				}

				int rows = pagenitionService.countSQL(searchCondition);
				if (rows > 0) {
					searchRsList = pagenitionService.findAllSQL(
							searchCondition, calcuStart(getCurrentPage()),
							Constance.PAGE_MAX_SIZE);
				}

				List<TblSettleItem> settleList = new ArrayList<TblSettleItem>();
				dealTheSearchRsList(searchRsList, settleList);
				addActionMessage("当前条件下统计到【" + rows + "】条记录");
				getRequest().setAttribute("settleList", settleList);
				this.setTotalPage(this.calcuTotalPage(rows));
				getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
				getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
				setSearchToConditionSession(searchCondition);
				return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("临时强扣查询异常");
			return ERROR;
		}
		return ERROR;
	}
	
	
	/**
	 * 跳转临时强扣登记页面
	 * 
	 * @return
	 */
	@Action(value = "addStrongDeduc", results = {
			@Result(name = "success", location = "/pages/settlement/addStrongDeduc.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String addStrongDeduc() {
		logger.info("初始化临时强扣登记页面");
		return SUCCESS;
	}
	
	/**
	 * 保存强扣出款信息
	 * 
	 * @return
	 */
	@Action(value = "saveDeducInfo", results = {
			@Result(name = "success", location = "/pages/settlement/tmpStrongDecductions.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String saveStrongDedu(){
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		Map<String,Object> resMap = new HashMap<String, Object>();
		try {
			resMap = tmpStrongDeducService.saveDeducInfo(logingMerchant, freezeBean);
			addActionMessage((String) resMap.get("msg"));
		} catch (Exception e) {
			addActionMessage("操作异常");
			logger.error("保存临时强扣登记信息异常："+e);
		}
		
		return SUCCESS;
	}
	
	private void dealTheSearchRsList(List<Object> searchRsList,
			List<TblSettleItem> settleList) throws Exception {
		try {
			if(searchRsList != null && searchRsList.size() > 0){
				Iterator<Object> it = searchRsList.iterator();
				while(it.hasNext()){
					Object[] obj = (Object[]) it.next();
					TblSettleItem bean = new TblSettleItem();
					bean.setId(Long.parseLong(Constance.Null2EmptyTrim(obj[0])));
					bean.setInsIdCd(Constance.Null2EmptyTrim(obj[1]));
					bean.setMchtCd(Constance.Null2EmptyTrim(obj[2]));
					bean.setMchtName(Constance.Null2EmptyTrim(obj[3]));
					String transAmt = Constance.Null2EmptyTrim(obj[4]);
					if(!"".equals(transAmt)){
						bean.setTransAmt(Double.parseDouble(transAmt));
					}else{
						bean.setTransAmt(0.00);
					}
					String totalNum = Constance.Null2EmptyTrim(obj[5]); 
					if(!"".equals(totalNum)){
						bean.setTotalNum(Integer.parseInt(totalNum));
					}else{
						bean.setTotalNum(0);
					}
					String mchtFee = Constance.Null2EmptyTrim(obj[6]);
					if(!"".equals(mchtFee)){
						bean.setMchtFee(Double.parseDouble(mchtFee));
					}else{
						bean.setMchtFee(0.00);
					}
					String setAmt = Constance.Null2EmptyTrim(obj[7]);
					if(!"".equals(setAmt)){
						bean.setMchtSetAmt(Double.parseDouble(setAmt));
					}else{
						bean.setMchtSetAmt(0.00);
					}
					bean.setBankName(Constance.Null2EmptyTrim(obj[8]));
					bean.setBankCode(Constance.Null2EmptyTrim(obj[9]));
					bean.setAccount(Constance.Null2EmptyTrim(obj[10]));
					bean.setAccountName(Constance.Null2EmptyTrim(obj[11]));
					bean.setAccType(Constance.Null2EmptyTrim(obj[12]));
					bean.setSettleDate(Constance.Null2EmptyTrim(obj[13]));
					bean.setSettleFlag(Constance.Null2EmptyTrim(obj[14]));
					bean.setSetDate(Constance.Null2EmptyTrim(obj[15]));
					bean.setFreezeFlag(Constance.Null2EmptyTrim(obj[16]));
					String freezeAmt = Constance.Null2EmptyTrim(obj[19]);
					if(!"".equals(freezeAmt)){
						bean.setFreezeAmt(Double.parseDouble(freezeAmt));
					}else{
						bean.setFreezeAmt(0.00);
					}
					String unfreezeAmt = Constance.Null2EmptyTrim(obj[20]);
					if(!"".equals(unfreezeAmt)){
						bean.setUnFreezeAmt(Double.parseDouble(unfreezeAmt));;
					}else{
						bean.setUnFreezeAmt(0.00);
					}
					bean.setRemark1(Constance.Null2EmptyTrim(obj[21]));
					bean.setRemark(Constance.Null2EmptyTrim(obj[22]));
					settleList.add(bean);
				}
			}
		}catch (Exception e) {
			throw new Exception("解析数据封装到待出款信息实体类异常");
		}
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
		
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_TMP_DEDUCTIONS_QUERY, searchCondition);
		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (BaseSearchCondition)getSession().getAttribute(SEARCH_SESSION_TMP_DEDUCTIONS_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		return Constance.PAGE_MAX_SIZE+"";
	}

	public TemStrongDeductionSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(TemStrongDeductionSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	public FreezeBean getFreezeBean() {
		return freezeBean;
	}
	public void setFreezeBean(FreezeBean freezeBean) {
		this.freezeBean = freezeBean;
	}
	
	

}
